package cn.com.jinhao.provider.business.controller;

import cn.com.jinhao.hotline.commons.controller.BaseController;
import cn.com.jinhao.hotline.commons.exception.ParamErrorException;
import cn.com.jinhao.hotline.commons.util.RestResponse;
import cn.com.jinhao.hotline.commons.vo.business.CancelWorkOrderAuditRequestVO;
import cn.com.jinhao.hotline.commons.vo.business.CancelWorkOrderRequestVO;
import cn.com.jinhao.provider.business.entity.WorkOrderCanceled;
import cn.com.jinhao.provider.business.service.CancelWorkOrderService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * hotline-spring-cloud
 * <p>Description: 撤单控制器</p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2020/4/8
 * @since 2020-04-08
 */

@RestController
@RequestMapping(value = "/cancelWorkOrder")
@Api(tags = {"撤单服务"})
public class CancelWorkOrderController extends BaseController {
    @Resource
    private CancelWorkOrderService cancelWorkOrderService;

    @ApiOperation(value = "撤单", notes = "新增撤单")
    @ApiParam(name = "workOrderCanceled", value = "撤单信息", required = true)
    @PostMapping
    public RestResponse<String> add(@Validated @RequestBody CancelWorkOrderRequestVO cancelWorkOrderRequestVO, BindingResult result) throws Exception {
        validate(result);
        WorkOrderCanceled workOrderCanceled = new WorkOrderCanceled();
        BeanUtils.copyProperties(cancelWorkOrderRequestVO,workOrderCanceled);
        cancelWorkOrderService.add(workOrderCanceled);
        return new RestResponse<>(null);
    }

    @ApiOperation(value="撤单审核列表", notes = "撤案审核列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptCode", value = "部门编号", required = true),
            @ApiImplicitParam(name = "isAll", value = "是否全部",dataType = "Boolean", required = true, defaultValue = "false", allowableValues = "true,false"),
            @ApiImplicitParam(name = "pageNo", value = "页数", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录条数", defaultValue = "20")
    })
    @GetMapping(value = "/aduitList")
    public RestResponse<java.util.Map<String, Object>> auditList(@Validated @RequestParam String deptCode, @RequestParam boolean isAll, @RequestParam int pageNo, @RequestParam int pageSize) throws Exception {
        if (!isAll) {
            if (pageNo <= 0) {
                throw new ParamErrorException("pageNo必须大于0");
            }
            if (pageSize <= 0) {
                throw new ParamErrorException("pageSize必须大于0");
            }
            return new RestResponse<>(cancelWorkOrderService.listAuditByCancel(deptCode, pageNo, pageSize));
        }else {
            return new RestResponse<>( cancelWorkOrderService.listAuditByCancel(deptCode, 0, 0));
        }
    }

    @ApiOperation(value="审核", notes = "撤单审核")
    @ApiParam(name = "cancelWorkOrderAuditRequestVO", value="撤单审核请求信息", required = true)
    @PutMapping
    public RestResponse<String> updateAudit(@Validated @RequestBody CancelWorkOrderAuditRequestVO cancelWorkOrderAuditRequestVO, BindingResult result) throws Exception {
        validate(result);
        WorkOrderCanceled workOrderCanceled = new WorkOrderCanceled();
        BeanUtils.copyProperties(cancelWorkOrderAuditRequestVO,workOrderCanceled);
        cancelWorkOrderService.audit(workOrderCanceled);
        return new RestResponse<>(null);
    }

    @ApiOperation(value ="撤单详情", notes = "撤单详情")
    @ApiParam(name = "code", value = "撤单编码", required = true)
    @GetMapping("/detail")
    public RestResponse<WorkOrderCanceled> detail(@RequestParam String code) throws Exception {
        return new RestResponse<>(cancelWorkOrderService.detail(code));
    }

    @ApiOperation(value = "撤单历史信息", notes = "撤单历史信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "dispatchCode", value = "派单编码", required = true),
        @ApiImplicitParam(name = "isAll", value = "是否全部",dataType = "Boolean", required = true, defaultValue = "false", allowableValues = "true,false"),
        @ApiImplicitParam(name = "pageNo", value = "页数", defaultValue = "1"),
        @ApiImplicitParam(name = "pageSize", value = "记录条数", defaultValue = "20")
    })
    @GetMapping("/history")
    public RestResponse<java.util.Map<String, Object>> history(@RequestParam String dispatchCode, @RequestParam boolean isAll, @RequestParam int pageNo, @RequestParam int pageSize) throws Exception {
        if (!isAll) {
            if (pageNo <= 0) {
                throw new ParamErrorException("pageNo必须大于0");
            }
            if (pageSize <= 0) {
                throw new ParamErrorException("pageSize必须大于0");
            }
            return new RestResponse<>(cancelWorkOrderService.queryHistory(dispatchCode, pageNo, pageSize));
        }else {
            return new RestResponse<>(cancelWorkOrderService.queryHistory(dispatchCode, 0, 0));
        }
    }
}
